package sjtu.ist.warehouse4ws.tool;

import java.io.File;
import java.util.ArrayList;
import java.util.Map;

import org.apache.struts2.ServletActionContext;

import sjtu.ist.warehouse4ws.service.FileInfoService;
import sjtu.ist.warehouse4ws.service.PrivilegeService;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class ShowEpcModelXmlAction extends ActionSupport{
	PrivilegeService privilegeService;	
	public void setPrivilegeService(PrivilegeService privilegeService) {
		this.privilegeService = privilegeService;
	}
	/**
	 * 
	 */
	private static final long serialVersionUID = -5970906369937326326L;
	String modelXmlJson=null;
	ArrayList<String> list=new ArrayList<String>();

	public String showEpcXml(){

		ActionContext actionContext = ActionContext.getContext();
		Map session = actionContext.getSession();

		String user_id = session.get("user_id").toString();
		String user_company = session.get("user_company").toString();
		int user_type = (Integer)(session.get("user_type"));
	
		String path=ServletActionContext.getServletContext().getRealPath("/upload/files/").replaceAll("\\\\", "/");

		File dirfile=new File(path);
		File[] files=dirfile.listFiles();
		for(int i=0;i<files.length;i++){
			String str=files[i].getName();
			if(privilegeService.checkPrivilege(str, user_company, user_type, "read")){
				list.add(str);
			}
			
		}
		 modelXmlJson=showFunction(list);
		return SUCCESS;
	}
	public String showFunction(ArrayList<String> list){
		StringBuffer processjson = new StringBuffer();
		processjson.append("[");
		for(String function:list){
			processjson.append("{text:'");
			processjson.append(function);
			processjson.append("'");
			processjson.append(",leaf:true },");
			
		}
		processjson.deleteCharAt(processjson.length()-1);
		processjson.append("]");
	
		return processjson.toString();
	}
	public String getModelXmlJson() {
		return modelXmlJson;
	}
	public void setModelXmlJson(String modelXmlJson) {
		this.modelXmlJson = modelXmlJson;
	}
	public ArrayList<String> getList() {
		return list;
	}
	public void setList(ArrayList<String> list) {
		this.list = list;
	}

}
